#ifndef      __BSP_ILI9341_LCD_H
#define	     __BSP_ILI9341_LCD_H


#include "stm32f1xx.h"
#include "fonts.h"

/******************************* ILI9341 ???8080?????? ***************************/
/******?????******/
//??  
#define      ILI9341_CS_PORT               GPIOC
#define      ILI9341_CS_PIN                GPIO_PIN_4

//DC?? 
#define      ILI9341_DC_PORT               GPIOC
#define      ILI9341_DC_PIN                GPIO_PIN_7

//???
#define      ILI9341_WR_PORT               GPIOC
#define      ILI9341_WR_PIN                GPIO_PIN_6

//???  
#define      ILI9341_RD_PORT               GPIOC
#define      ILI9341_RD_PIN                GPIO_PIN_5

//????????NRST,??????????????


//????  
#define      ILI9341_BK_PORT               GPIOD
#define      ILI9341_BK_PIN                GPIO_PIN_2

/********?????***************/  
#define      ILI9341_DATA_PORT               GPIOB
#define      ILI9341_DATA_PIN                GPIO_PIN_All

/********?????????***************/
#define	ILI9341_CS_SET  	ILI9341_CS_PORT->BSRR=ILI9341_CS_PIN    //????  		
#define	ILI9341_DC_SET		ILI9341_DC_PORT->BSRR=ILI9341_DC_PIN    //??/?? 	  
#define	ILI9341_WR_SET		ILI9341_WR_PORT->BSRR=ILI9341_WR_PIN    //???			
#define	ILI9341_RD_SET		ILI9341_RD_PORT->BSRR=ILI9341_RD_PIN    //???			
								    
#define	ILI9341_CS_CLR  	ILI9341_CS_PORT->BRR=ILI9341_CS_PIN     //????  		
#define	ILI9341_DC_CLR		ILI9341_DC_PORT->BRR=ILI9341_DC_PIN     //??/??		     
#define	ILI9341_WR_CLR		ILI9341_WR_PORT->BRR=ILI9341_WR_PIN     //???			
#define	ILI9341_RD_CLR		ILI9341_RD_PORT->BRR=ILI9341_RD_PIN     //???			

//???????
#define DATAOUT(x) 	ILI9341_DATA_PORT->ODR=x; //????
#define DATAIN     	ILI9341_DATA_PORT->IDR;   //????	


/*************************************** ???? ******************************************/
#define      DEBUG_DELAY()       ILI9341_Delay(0xFFFF)         

/***************************** ILI934 ?????????????? ***************************/
#define      ILI9341_DispWindow_X_Star		    0     //????X??
#define      ILI9341_DispWindow_Y_Star		    0     //????Y??

#define 			ILI9341_LESS_PIXEL	  							240			//????????????
#define 			ILI9341_MORE_PIXEL	 								320			//????????????

//????????????XY????
//??ILI9341_GramScan????????????
extern uint16_t LCD_X_LENGTH,LCD_Y_LENGTH; 

//???????
//??????0-7
extern uint8_t LCD_SCAN_MODE;

/******************************* ?? ILI934 ??????? ********************************/
#define      BACKGROUND		                 BLACK     //??????

#define      WHITE		 		                 0xFFFF	   //??
#define      BLACK                         0x0000	   //?? 
#define      GREY                          0xF7DE	   //?? 
#define      BLUE                          0x001F	   //?? 
#define      BLUE2                         0x051F	   //??? 
#define      RED                           0xF800	   //?? 
#define      MAGENTA                       0xF81F	   //???,??? 
#define      GREEN                         0x07E0	   //?? 
#define      CYAN                          0x7FFF	   //???,?? 
#define      YELLOW                        0xFFE0	   //?? 
#define      BRED                          0xF81F
#define      GRED                          0xFFE0
#define      GBLUE                         0x07FF



/******************************* ?? ILI934 ???? ********************************/
#define      CMD_SetCoordinateX		 		    0x2A	     //??X??
#define      CMD_SetCoordinateY		 		    0x2B	     //??Y??
#define      CMD_SetPixel		 		          0x2C	     //????


/* ?? LCD ???? ID */
#define     LCDID_UNKNOWN             0
#define     LCDID_ILI9341             0x9341
#define     LCDID_ST7789V             0x8552


/********************************** ?? ILI934 ?? ***************************************/
void                     ILI9341_Init                    ( void );
uint16_t                 ILI9341_ReadID                  ( void );
void                     ILI9341_Rst                     ( void );
void                     ILI9341_BackLed_Control         ( FunctionalState enumState );
void                     ILI9341_GramScan                ( uint8_t ucOtion );
void                     ILI9341_OpenWindow              ( uint16_t usX, uint16_t usY, uint16_t usWidth, uint16_t usHeight );
void                     ILI9341_Clear                   ( uint16_t usX, uint16_t usY, uint16_t usWidth, uint16_t usHeight );
void                     ILI9341_SetPointPixel           ( uint16_t usX, uint16_t usY );
uint16_t                 ILI9341_GetPointPixel           ( uint16_t usX , uint16_t usY );
void                     ILI9341_DrawLine                ( uint16_t usX1, uint16_t usY1, uint16_t usX2, uint16_t usY2 );
void                     ILI9341_DrawRectangle           ( uint16_t usX_Start, uint16_t usY_Start, uint16_t usWidth, uint16_t usHeight,uint8_t ucFilled );
void                     ILI9341_DrawCircle              ( uint16_t usX_Center, uint16_t usY_Center, uint16_t usRadius, uint8_t ucFilled );
void                     ILI9341_DispChar_EN             ( uint16_t usX, uint16_t usY, const char cChar );
void                     ILI9341_DispStringLine_EN       ( uint16_t line, char * pStr );
void                     ILI9341_DispString_EN      	 ( uint16_t usX, uint16_t usY, char * pStr );
void 					 ILI9341_DispString_EN_YDir 	 (   uint16_t usX,uint16_t usY ,  char * pStr );


void 											LCD_SetFont											(sFONT *fonts);
sFONT 										    *LCD_GetFont											(void);
void 											LCD_ClearLine										(uint16_t Line);
void 											LCD_SetBackColor								(uint16_t Color);
void 											LCD_SetTextColor								(uint16_t Color)	;
void 											LCD_SetColors										(uint16_t TextColor, uint16_t BackColor);
void 											LCD_GetColors										(uint16_t *TextColor, uint16_t *BackColor);
uint16_t 								        ILI9341_Read_ID									(void);


/* ????????????IO */
#define	digitalH(p,i)			{p->BSRR=i;}			  //??????		
#define digitalL(p,i)			{p->BSRR=(uint32_t)i << 16;}				//?????

#endif /* __BSP_ILI9341_ILI9341_H */
